package com.whjz.dcfs.bdzRealTimeData.struts.action;

import com.alibaba.fastjson.JSON;
import com.whjz.dcfs.bdzRealTimeData.dao.IBdzDataDAO;
import com.whjz.dcfs.bdzRealTimeData.model.BdzRealTimeData;
import com.whjz.dcfs.bdzRealTimeData.model.entity.BdzRealTimeListResp;
import com.whjz.dcfs.jzxx.dao.IDcfsHjjcdJbxxDAO;
import com.whjz.dcfs.jzxx.model.DcfsHjjcdJbxx;
import com.whjz.dcfs.ssjk.dao.IDcfsHsEmstimeDAO;
import com.whjz.dcfs.ssjk.model.*;
import com.whjz.dcfs.util.BaseAction;
import com.whjz.dcfs.util.JsonUtils;
import com.whjz.dcfs.util.UserUtil;
import com.whjz.dcfs.util.enums.BdzShiFanEnum;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

public class BdzRealTimeDataAction extends BaseAction{

	private static final long serialVersionUID = 1L;

	private static Logger logger = LoggerFactory.getLogger(BdzRealTimeDataAction.class);

	@Autowired
	public IDcfsHjjcdJbxxDAO dcfsHjjcdJbxxDAO;
	@Autowired
	public IDcfsHsEmstimeDAO dcfsHsEmstimeDAO;
	@Autowired
	private IBdzDataDAO bdzDataDAO;

	private DcfsHjjcdJbxx dcfsHjjcdJbxx;
	private DcfsHsEmstime dcfsHsEmstime;
	private String chartJson;
	private String flag;//1为查询区域，2为查询站点
	private List<StoreY> storeY;
	private String time;
	private Store store;
	private String bdzCode;
	private Integer startRowNum;
	private Integer endRowNum;

	public void getDcfsHjjcdJbxxListByCond() throws Exception{
		String xzqh=UserUtil.findUserFromSession(getSession().get("user"),"getConcern");
		if(xzqh.indexOf("区")!=-1){
			dcfsHjjcdJbxx=new DcfsHjjcdJbxx();
			dcfsHjjcdJbxx.setHjzljkxzqh(xzqh);
		}
		List<HashMap> list=dcfsHjjcdJbxxDAO.findXzqy(dcfsHjjcdJbxx);
		getHttpServletResponse().setCharacterEncoding("utf-8");
		StringBuffer sb=new StringBuffer();
		sb.append("[{");
		sb.append("\"id\":\"test\",");
		sb.append("\"text\":\"监测站点\",");
		sb.append("\"cls\":\"folder\",");
		sb.append("\"expanded\":true,");
		sb.append("\"children\":[");
		if(list.size()>0){
			for (int m=0;m<list.size();m++) {
				sb.append("{");
				sb.append("\"text\":\""+list.get(m).get("xzqh")+"("+list.get(m).get("num")+")"+"\",");
				dcfsHjjcdJbxx=new DcfsHjjcdJbxx(); 
				dcfsHjjcdJbxx.setHjzljkxzqh(list.get(m).get("xzqh").toString());
				List<DcfsHjjcdJbxx> listall=dcfsHjjcdJbxxDAO.getDcfsHjjcdJbxxListByCond(dcfsHjjcdJbxx);
//				String mns="";
//				for (DcfsHjjcdJbxx dcfsHjjcdJbxx : listall) {
//					mns=mns+","+dcfsHjjcdJbxx.getJkxtmn();
//				}
				sb.append("\"id\":\""+list.get(m).get("xzqh").toString()+"\",");
				sb.append("\"cls\":\"folder\",");
				sb.append("\"expanded\":false,");
				sb.append("\"children\":[");
				int j=1;
				if(listall!=null&&listall.size()>0){
					for(int i=0;i<listall.size();i++){
						sb.append("{");
						sb.append("\"text\":\""+(listall.get(i).getJcdmc()==null?"":listall.get(i).getJcdmc())+"\",");
						sb.append("\"id\":\""+(listall.get(i).getJkxtmn()==null?"":listall.get(i).getJkxtmn())+"\",");
						sb.append("\"leaf\":true");
						if(j<listall.size()){
							sb.append("},");
						}else{
							sb.append("}]");
						}
						j++;
				}
				}
				if(m<(list.size()-1)){
					sb.append("},");
				}else{
					sb.append("}");
				}
			}
		}
		sb.append("]}]");
		getHttpServletResponse().getWriter().println(sb);
	}
	
	public void getDcfsHsEmstimeListByCond() throws Exception{
		List<DcfsHsEmstime> list;
//		SimpleDateFormat formatS=new SimpleDateFormat("yyyyMMddhhmmss");
//		SimpleDateFormat formatD=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if("1".equals(flag)){
//			String mns=dcfsHjjcdJbxx.getJkxtmn().substring(1, dcfsHjjcdJbxx.getJkxtmn().length());
//			String[] MnStr=mns.split(",");
			list=dcfsHsEmstimeDAO.getDcfsHsEmstimeListByCondNew(dcfsHjjcdJbxx.getJkxtmn().toString());
		}else{
			list=dcfsHsEmstimeDAO.getDcfsHsEmstimeListByCond(dcfsHsEmstime);
		}
		StringBuffer sb=new StringBuffer();
		sb.append("{root:[");
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				sb.append("{");
				sb.append("Jcd_mc:'"+list.get(i).getJcdmc()+"',");
				sb.append("JKXT_MN:'"+list.get(i).getJkxtmn()+"',");
				sb.append("Datatime:'"+list.get(i).getDatatime()+"',");
				if(list.get(i).getAvalue()==null){
					sb.append("Avalue:'"+list.get(i).getAvalue()+"',");
				}else{
					sb.append("Avalue:'"+Double.valueOf(list.get(i).getAvalue())+"',");
				}
				sb.append("Flag:'"+list.get(i).getFlag()+"',");
				sb.append("DataState:'"+list.get(i).getDatastate()+"'");
				sb.append("}");
				if(i < list.size()-1){
					sb.append(",");
				}
			}
		}
	    sb.append("]}");
//	    System.out.println("sb---"+sb);
	    getHttpServletResponse().setCharacterEncoding("utf-8");
	    getHttpServletResponse().setContentType("text/html:utf-8");
	    getHttpServletResponse().getWriter().print(sb);
	}
	
	public String chart() throws Exception{
		List<DcfsHsEmstime> list=dcfsHsEmstimeDAO.getDcfsHsEmstimeListByCond(dcfsHsEmstime);
//		SimpleDateFormat formatS=new SimpleDateFormat("yyyyMMddhhmmss");
//		SimpleDateFormat formatD=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		chartJson="[";
		for(int i=0;i<list.size();i++){
			chartJson+="{\"Datatime\":\""+list.get(i).getDatatime()+"\",";
			chartJson+="\"数值\":"+list.get(i).getAvalue();
			if(i==(list.size()-1)){
				chartJson+="}]";
			}else{
				chartJson+="},";
			}
		}
		return "chart";
	}

	//判断是否达标
	private void setIsValidStr(BdzRealTimeData erms) {
		if(erms!=null) {
			if (erms.getMesure()!=null ) {
				if(4000 >= erms.getMesure()) {
					erms.setIsMesureValid("达标");
				}else {
					erms.setIsMesureValid("不达标");
				}
			}else{
				erms.setIsMesureValid("-");
			}
			if (erms.getMesure2()!=null ) {
				if(100 >= erms.getMesure2()) {
					erms.setIsMesure2Valid("达标");
				}else {
					erms.setIsMesure2Valid("不达标");
				}
			}else{
				erms.setIsMesureValid("-");
			}
		}
	}

	//示范站点实时数据
	public void getRealTimeList() throws Exception{
		List<BdzRealTimeData> list = bdzDataDAO.getBdzDataList();
		BdzRealTimeListResp bdzRealTimeListResp = new BdzRealTimeListResp();
		if(list!=null) {
			for (BdzRealTimeData bdzRealTimeData : list) {
				//根据bdzCode设置对应的bdzName
				bdzRealTimeData.setBdzName(BdzShiFanEnum.getBdzNameByBdzCode(bdzRealTimeData.getBdzCode()));
				//设置达标字段
				setIsValidStr(bdzRealTimeData);
			}
			bdzRealTimeListResp.setRoot(list);
		}else{
			bdzRealTimeListResp.setRoot(Collections.EMPTY_LIST);
		}
		String respJson = JSON.toJSONString(bdzRealTimeListResp);
		logger.info("示范站点实时数据列表respJson={}",respJson);
	    getHttpServletResponse().setCharacterEncoding("utf-8");
	    getHttpServletResponse().setContentType("text/html:utf-8");
	    getHttpServletResponse().getWriter().print(respJson);
	}

	public void getRealTimeListByCond() throws Exception {
		Map map = new HashMap();
		map.put("bdzCode", bdzCode);
		if(startRowNum!=null) {
			map.put("startRowNum", startRowNum);
			map.put("endRowNum", endRowNum);
		}else{
			map.put("startRowNum", 0);
			map.put("endRowNum", 15);
		}
		List<BdzRealTimeData> list = bdzDataDAO.getRealTimeDataByBdzCode(map);
		BdzRealTimeListResp bdzRealTimeListResp = new BdzRealTimeListResp();
		if(list!=null) {
			for (BdzRealTimeData bdzRealTimeData : list) {
				//根据bdzCode设置对应的bdzName
				bdzRealTimeData.setBdzName(BdzShiFanEnum.getBdzNameByBdzCode(bdzRealTimeData.getBdzCode()));
				//设置达标字段
				setIsValidStr(bdzRealTimeData);
			}
			bdzRealTimeListResp.setRoot(list);
		}else{
			bdzRealTimeListResp.setRoot(Collections.EMPTY_LIST);
		}
		String respJson = JSON.toJSONString(bdzRealTimeListResp);
		logger.info("示范站点实时数据列表respJson={}",respJson);
		getHttpServletResponse().setCharacterEncoding("utf-8");
		getHttpServletResponse().setContentType("text/html:utf-8");
		getHttpServletResponse().getWriter().print(respJson);
	}

	//示范点实时图
	public void getsssjData() throws Exception{
		Map map = new HashMap();
		map.put("bdzCode", bdzCode);
		map.put("startRowNum", 0);
		map.put("endRowNum", 15);
		List<BdzRealTimeData> list = bdzDataDAO.getRealTimeDataByBdzCode(map);
		storeY=new ArrayList<StoreY>();
		String str=getData(list,"now");
		getHttpServletResponse().setCharacterEncoding("utf-8");
		getHttpServletResponse().setContentType("text/html:utf-8");
		getHttpServletResponse().getWriter().print(str);
	}

	public String getData(List<BdzRealTimeData> list,String type){
		store=new Store();
		StoreY storeY1=new StoreY();
		storeY1.setName("工频电场");
		StoreY storeY2=new StoreY();
		storeY2.setName("工频磁场");

		double[] value1=new double[list.size()];
		double[] value2=new double[list.size()];

		String[] x=new String[list.size()];
		for(int i=0;i<list.size();i++){
			value1[i]=list.get(i).getMesure();
			value2[i]=list.get(i).getMesure2();

			if("week".equals(type)){
				x[i]=list.get(i).getDataTime().split(" ")[0];
			}else if("month".equals(type)){
				x[i]=String.valueOf(i+1);
			}else if("now".equals(type)){
				x[i]=list.get(i).getDataTime();
			}else if("day".equals(type)){
				x[i]=String.valueOf(i);
			}
		}
		storeY1.setData(value1);
		storeY2.setData(value2);

		storeY.add(storeY1);
		storeY.add(storeY2);

		store.setStoreY(storeY);
		StoreX storeX=new StoreX();
		storeX.setCategories(x);
		if("now".equals(type)){
			Labels labels=new Labels();
			labels.setEnabled(false);
			storeX.setLabels(labels);
		}
		store.setStoreX(storeX);
		return JsonUtils.toJsonString(store);
	}






	public DcfsHjjcdJbxx getDcfsHjjcdJbxx() {
		return dcfsHjjcdJbxx;
	}

	public void setDcfsHjjcdJbxx(DcfsHjjcdJbxx dcfsHjjcdJbxx) {
		this.dcfsHjjcdJbxx = dcfsHjjcdJbxx;
	}

	public DcfsHsEmstime getDcfsHsEmstime() {
		return dcfsHsEmstime;
	}

	public void setDcfsHsEmstime(DcfsHsEmstime dcfsHsEmstime) {
		this.dcfsHsEmstime = dcfsHsEmstime;
	}

	public String getChartJson() {
		return chartJson;
	}

	public void setChartJson(String chartJson) {
		this.chartJson = chartJson;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getBdzCode() {
		return bdzCode;
	}

	public void setBdzCode(String bdzCode) {
		this.bdzCode = bdzCode;
	}

	public Integer getStartRowNum() {
		return startRowNum;
	}

	public void setStartRowNum(Integer startRowNum) {
		this.startRowNum = startRowNum;
	}

	public Integer getEndRowNum() {
		return endRowNum;
	}

	public void setEndRowNum(Integer endRowNum) {
		this.endRowNum = endRowNum;
	}
}
